package com.ruoyi.docdemo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.doc.util.RyDocUtil;
import com.ruoyi.docdemo.domain.DemoStudent;
import com.ruoyi.docdemo.service.IDemoStudentService;

/**
 * 学生档案Controller
 * 
 * @author ruoyi
 * @date 2021-01-09
 */
@RestController
@RequestMapping("/docdemo/student")
public class DemoStudentController extends BaseController
{
    @Autowired
    private IDemoStudentService demoStudentService;

    /**
     * 查询学生档案列表
     */
    @PreAuthorize("@ss.hasPermi('docdemo:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(DemoStudent demoStudent)
    {
        startPage();
        List<DemoStudent> list = demoStudentService.selectDemoStudentList(demoStudent);
        return getDataTable(list);
    }

    /**
     * 导出学生档案列表
     */
    @PreAuthorize("@ss.hasPermi('docdemo:student:export')")
    @Log(title = "学生档案", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DemoStudent demoStudent)
    {
        List<DemoStudent> list = demoStudentService.selectDemoStudentList(demoStudent);
        ExcelUtil<DemoStudent> util = new ExcelUtil<DemoStudent>(DemoStudent.class);
        return util.exportExcel(list, "student");
    }

    /**
     * 获取学生档案详细信息
     */
    @PreAuthorize("@ss.hasPermi('docdemo:student:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(demoStudentService.selectDemoStudentById(id));
    }

    /**
     * 新增学生档案
     */
    @PreAuthorize("@ss.hasPermi('docdemo:student:add')")
    @Log(title = "学生档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DemoStudent demoStudent)
    {
        return toAjax(demoStudentService.insertDemoStudent(demoStudent));
    }

    /**
     * 修改学生档案
     */
    @PreAuthorize("@ss.hasPermi('docdemo:student:edit')")
    @Log(title = "学生档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DemoStudent demoStudent)
    {
        return toAjax(demoStudentService.updateDemoStudent(demoStudent));
    }

    /**
     * 删除学生档案
     */
    @PreAuthorize("@ss.hasPermi('docdemo:student:remove')")
    @Log(title = "学生档案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(demoStudentService.deleteDemoStudentByIds(ids));
    }
    

    /**
     * 导出学生档案word
     * @throws Exception 
     */
    @PreAuthorize("@ss.hasPermi('docdemo:student:export')")
    @Log(title = "学生档案", businessType = BusinessType.EXPORT)
    @GetMapping("/exportDoc/{ids}/{fileType}")
    public AjaxResult exportDoc(@PathVariable Long[] ids,@PathVariable String fileType) throws Exception
    {
    	// 1、查询表数据
		DemoStudent stu = demoStudentService.selectDemoStudentById(ids[0]);
		
		// 2、如果存在字典类字段，需要手工转换，比如性别，0=男，1=女
		if(stu.getSex().equalsIgnoreCase("0")) {
			stu.setSex("男");
		} else {
			stu.setSex("女");
		}

		// 3、将实体类转为map，注意此处只是为了方便直接做了转换，实际上可以组装任何map类型的数据
		Map<String,Object> dataMap = JSON.parseObject(JSON.toJSONString(stu), Map.class);
		
		// 4、map中加入实体以外的数据变量，如果是对象，在word中使用.进行索引取值
		// 4.1、普通变量
		dataMap.put("otherData", "其它数据值");
		dataMap.put("birthDateValue", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, stu.getBirthDate()));
		// 4.2、对象变量
		Map<String,Object> courseDataMap = new HashMap<String,Object>();
		courseDataMap.put("cname", "历史课程");
		courseDataMap.put("goal", "83");
		dataMap.put("courseData", courseDataMap);
		// 4.3、列表变量（与实体数据是一对多的关系，比如学生的所修课程信息，赞助版支持）
		
		// 文档类型
		String fileName = RyDocUtil.getDocFileName("student_doc", fileType, "学生档案-"+stu.getName(), dataMap);
		
		return AjaxResult.success(fileName);
    	
    }
}
